﻿using FriendAnalyzer.Business.Model;
using FriendAnalyzer.Infrastructure.Facebook.Model;
using System.Collections.Generic;
using System.Linq;

namespace FriendAnalyzer.Business.FacebookFilter
{
    public abstract class BaseAggregationStrategy
    {
        protected List<AggregateResult> ParseCriteriaList(IEnumerable<AggregateCriteria> criterias)
        {
            var map = from criteria in criterias
                      where criteria.Matches.Any()
                      let parent = criteria.Matches.FirstOrDefault()
                      from other in criteria.Matches
                      select new AggregateResult
                      {
                          User1 = parent,
                          User2 = other,
                          ItemsInCommon = 1,
                      };

            var reduce = from item in map
                         group item by new { item.User1, item.User2 } into grp
                         select new AggregateResult
                         {
                             User1 = grp.Key.User1,
                             User2 = grp.Key.User2,
                             ItemsInCommon = grp.Count(),
                         };

            return reduce.ToList();
        }

        public abstract List<AggregateResult> AggregateData(List<FacebookUser> users);
    }
}
